package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.TenderDictEntity3;
import com.sgb.kjwl.model.entity.pick.TenderInfoBasicEntity3;
import com.swgk.core.recyclerview.secondary.DescHolder;
import com.swgk.core.recyclerview.secondary.HeaderHolder;
import com.swgk.core.recyclerview.secondary.SectionedRecyclerViewAdapter;

import java.util.List;

public class PickNoticeDetailItemAdapter extends SectionedRecyclerViewAdapter<TenderInfoBasicEntity3, TenderDictEntity3, HeaderHolder, DescHolder, RecyclerView.ViewHolder> {

    public PickNoticeDetailItemAdapter(Context context, List<TenderInfoBasicEntity3> datas) {
        super(context, R.layout.item_pre_pick_notice_request, R.layout.item_pre_pic_notice_request_item, datas);
    }

    @Override
    protected void onBindSectionHeaderViewHolder(HeaderHolder holder, int section, TenderInfoBasicEntity3 secondaryEntity) {
        holder.setBinding(BR.viewModel, secondaryEntity);
    }

    @Override
    protected void onBindSectionFooterViewHolder(RecyclerView.ViewHolder holder, int section) {

    }

    @Override
    protected void onBindItemViewHolder(DescHolder holder, int section, int position, TenderDictEntity3 entity) {
        holder.setBinding(BR.viewModel, entity);

        TextView contentTitle = holder.getItem().findViewById(R.id.content_title);
        TextView tv_expand = holder.getItem().findViewById(R.id.expand_view);
        TextView tv_survey = holder.getItem().findViewById(R.id.survey_view);

        tv_survey.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                tv_survey.getViewTreeObserver().removeOnPreDrawListener(this);
                int line = tv_survey.getLineCount();
                if (line <= 3) {
                    tv_expand.setVisibility(View.GONE);
                }
                return false;
            }
        });

        tv_expand.setOnClickListener(view -> {

            if (tv_expand.getText().equals("展开")) {
                tv_survey.setMaxLines(Integer.MAX_VALUE);
                tv_expand.setText("收起");
            } else {
                tv_survey.setMaxLines(3);
                tv_expand.setText("展开");
            }
        });

        String nameItem = entity.getName();
        if(!TextUtils.isEmpty(nameItem)){
            int length = nameItem.length();
            if(length > 5){
                int mult = length / 4;
                if(mult == 0){
                    contentTitle.setText(nameItem);
                } else {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mult; i++) {
                        sb.append(nameItem.substring(4 * i, 4 * i + 4));
                        if (i < mult - 1) {
                            sb.append("\n");
                        }
                    }
                    if (mult > 0 && length % 4 > 0) {
                        sb.append("\n");
                        sb.append(nameItem.substring(mult * 4));
                    }
                    contentTitle.setText(sb.toString());
                }
            }else{
                contentTitle.setText(nameItem);
            }
        }
    }
}
